<?php
/**
 * @Author:      zwh
 * @DateTime:    2016-10-9 10:36:50
 * @Description: 用于 门店盘点包设置
 */
class ScsInventoryConditionController extends BaseController
{
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 16:16:11
     * @Description: 盘点包列表
     */
    public function lists(){
    
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ic=InventoryCondition::leftJoin('sc_user as sc1','sc1.id','=','inventory_condition.created_by')
                ->leftJoin('sc_user as sc2','sc2.id','=','inventory_condition.updated_by')
                ->where('inventory_condition.shop_id',$sc_user->shop_id)
                ->where('inventory_condition.status',1)
                ->select('inventory_condition.id','inventory_condition.name','inventory_condition.dimension','inventory_condition.description','inventory_condition.created_at','inventory_condition.updated_at','sc1.real_name as create_user','sc2.real_name as update_user');
            if(Input::get('name')){
                $ic->where('name','like','%'.Input::get('name').'%');
            }
            $ic=$ic->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("ic"=>$ic));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 15:57:13
     * @Description: 分类列表
     */
    public function classification(){

    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ic=InventoryCondition::find(Input::get('id'));
            $scf=SupplyClassification::where('level',$ic->dimension)->where('status',1)->select('id','name');
            if(Input::get('name')){
                $scf->where('name','like','%'.Input::get('name').'%');
            }
            $scf=$scf->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("scf"=>$scf));
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "classification", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 10:37:32
     * @Description: 创建盘点包
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'required|max:120',
                'dimension' => 'required',
                'description' => 'required'  
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ic=new InventoryCondition;
            $ic->name=Input::get('name');
            $ic->dimension=Input::get('dimension');
            $ic->description=Input::get('description');
            $ic->shop_id=$sc_user->shop_id;
            $ic->created_by=$sc_user->id;
            $ic->updated_by=$sc_user->id;
            $ic->save();
            $response['result']=array('id'=>$ic->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-17 13:51:48
     * @Description: 编辑盘点包前判断
     */
    public function editBefore(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $id=Input::get('id');
            $list_ids=MaintenanceInventoryList::whereIn('status',array(0,1))->where('shop_id',$sc_user->shop_id)->select('id')->get()->toArray();
            $list_ids=array_column($list_ids,'id');
            $ic_ids=InventoryListCondition::whereIn('list_id',$list_ids)->select('ic_id')->get()->toArray();
            $ic_ids=array_column($ic_ids,'ic_id');
            if(in_array($id,$ic_ids)){
                $response = Lang::get("scs_error.data_inventory_condition_error_61020");
                break;
            }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "editBefore", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 10:37:32
     * @Description: 编辑盘点包
     */
    public function edit(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ic=InventoryCondition::where('id',Input::get('id'))->where('status',1)->select('id','name','dimension','description')->first();
            if($ic->dimension==3){
                $ici=InventoryConditionItem::leftJoIn("supply_classification as sc3","sc3.id","=","inventory_condition_item.scf_id")
                    ->leftJoIn("supply_classification as sc2","sc2.id","=","sc3.parent_id")
                    ->leftJoIn("supply_classification as sc1","sc1.id","=","sc2.parent_id")
                    ->where('ic_id',Input::get('id'))
                    ->where('inventory_condition_item.status',1)
                    ->select('inventory_condition_item.id','sc1.name as one','sc2.name as two','sc3.name as name')
                    ->get();
            }else if($ic->dimension==2){
                $ici=InventoryConditionItem::leftJoIn("supply_classification as sc2","sc2.id","=","inventory_condition_item.scf_id")
                ->leftJoIn("supply_classification as sc1","sc1.id","=","sc2.parent_id")
                ->where('ic_id',Input::get('id'))
                ->where('inventory_condition_item.status',1)
                ->select('inventory_condition_item.id','sc1.name as one','sc2.name as name')
                ->get();
            }else{
                $ici=InventoryConditionItem::leftJoIn("supply_classification as sc1","sc1.id","=","inventory_condition_item.scf_id")
                ->where('ic_id',Input::get('id'))
                ->where('inventory_condition_item.status',1)
                ->select('inventory_condition_item.id','sc1.name as one','sc1.name as name')
                ->get();
            }
            
            $response['result']=array(
                'ic'=>$ic,
                'ici'=>$ici
            );
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "edit", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 10:37:32
     * @Description: 添加盘点包项目
     */
    public function addItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'scf_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ici=InventoryConditionItem::where('ic_id',Input::get('id'))->where('status',1)->select('scf_id')->get()->toArray();
            $scf_ids=array_column($ici,'scf_id');
            if(in_array(Input::get('scf_id'),$scf_ids)){
                $response = Lang::get("scs_error.data_inventory_error_61019");
                break;
            }
            
            $ici=new InventoryConditionItem;
            $ici->ic_Id=Input::get('id');
            $ici->scf_id=Input::get('scf_id');
            $ici->save();
            $response['result']=array('id'=>$ici->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-11 16:09:59
     * @Description: 编辑盘点包
     */
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'name' => 'required',
                'description' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ic=InventoryCondition::find(Input::get('id'));
            $ic->name=Input::get('name');
            $ic->description=Input::get('description');
            $ic->updated_by=$sc_user->id;
            $ic->save();
            $response['result']=array('id'=>$ic->id);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "update", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 16:07:38
     * @Description: 删除盘点包项目
     */
    public function deleteItem(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array());
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ici=InventoryConditionItem::find(Input::get('id'));
            $ici->status=0;
            $ici->save();
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deletePal", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-10-9 16:07:32
     * @Description: 删除盘点包
     */
    public function delete(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $id=Input::get('id');
            $response=DB::transaction(function () use ($id) {
                InventoryConditionItem::where('ic_id',$id)->update(array('status'=>0));
                $ic=InventoryCondition::find($id);
                $ic->status=0;
                $ic->save();
                return  array('code' => 0, 'msg' => 'ok', 'result' => array());
            });
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "delete", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-10-10 10:43:33
     * @Description: 
     */
    public function verification(){
        $validator = Validator::make(Input::all(),
            array(
                'token'    => 'required',
                'dimension' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        
        $response = array('code' => 0, 'msg' => 'ok','result'=>array('order_id'=>Input::get('order_id')));
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $ic=InventoryCondition::where('dimension',Input::get('dimension'))->where('status',1)->select('id')->get()->toArray();
            $ic=array_column($ic,'id');
            $ici=InventoryConditionItem::whereIn('ic_id',$ic)->where('status',1)->select('scf_id')->get()->toArray();
            $scf_ids=array_column($ici,'scf_id');
            $scfs=SupplyClassification::where('level',Input::get('dimension'))->where('status',1)->whereNotIn('id',$scf_ids);
            $count=$scfs->count();
            $scfs=$scfs->skip($page_size*($page-1))->take($page_size)->select('id','name')->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"scfs"=>$scfs));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "returnOrder", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}